xlv.imp.edc={
	implement:function(){
	},
	method:{
		edc_properties:{
			typeToStart:'mousedown',
			editKeyCode:113, //F2
			errorModal:false,
			source:null,
			sourceValue:'',
			editor:null
		},
		edc_enabled:function(enable){
			var type=this.edc_properties.typeToStart;
			if(enable){
				if(type=='mousedown')this.addEventHandler('mousedown', '_edc_start');
				if(type=='click')this.addEventHandler('click', '_edc_start');
				if(type=='dblclick')this.addEventHandler('dblclick', '_edc_start');
				if(type=='focus')this.addEventHandler('focus', '_edc_start');
				this.addEventHandler('keypress', '_edc_keypress');
			}else{
				if(type=='mousedown')this.addEventHandler('mousedown', '_edc_start');
				if(type=='click')this.removeObserve('click', '_edc_start');
				if(type=='dblclick')this.removeObserve('dblclick', '_edc_start');
				if(type=='focus')this.removeObserve('focus', '_edc_start');
				this.removeObserve('keypress', '_edc_keypress');
			}
		},
		_edc_start:function(event){
			/*if editor and edc are same and used by another edc, donot start again */
			if(this['editor_editing']&& this.editor_editing())return;
			var p=this.edc_properties;
			if(p.editor && xlv.util.isDescendantOf(event.target,p.editor))return;
			if(this.edc_editing()&&event.target!=p.editor) this.edc_complete(true);
			if(event.type==p.typeToStart){
				if(this.edc_edit(event.target)) event.stop();
			}	
		},
		_edc_keypress:function(event){
			var p=this.edc_properties;
			var c=(event.keyCode||event.charCode);
			switch(c){
				case p.editKeyCode:
					if(!this.edc_editing()){
						event.stop();
						this.edc_edit(event.target);
					}
					break;
			}
		},
		edc_edit:function(source){
			if(this.edc_editing()&&this.edc_complete(true)==false)return false;
			var source_element=this.edc_editable(source);
			if(!source_element)return false;
			var p=this.edc_properties;
			p.source=(source_element===true)? source:source_element;
			p.sourceValue=this.edc_valueFromSource(p.source);
			p._policy=Object.extend({},xlv.imp.editor.pub.policy);
			Object.extend(p._policy,(this.edc_policy(p.source)||{}));
			this._edc_storeSource(p._policy);
			var editor=this.edc_installEditor(p.sourceValue,p._policy);
			if(!p.editor && editor) p.editor=editor;
			if(!p.editor) return false;
			p.editor.editor_properties.originalValue=p.sourceValue;
			p.editor.editor_valueToEditor(p.sourceValue);
			p.editor.editor_addExitListener(this.edc_complete.bindAsEventListener(this));
			p.editor._editor_startEdit(p._policy);
			return true;
		},
		edc_complete:function(commit){
			if(!this.edc_editing())return true;
			var value,p=this.edc_properties;
			commit=(commit===true)?true:false;
			commit &= this.edc_validate();
			if(p.errorModal && !commit) {
				p.editor.editor_startEdit();
				return false;
			}
			if(commit) {
				value=p.editor.editor_valueFromEditor();
				if(p.sourceValue==value)commit=false;
			}
			p.editor.editor_clearExitListener();
			this.edc_disposeEditor(p.editor,p._policy);
			this.edc_uninstallEditor(p.editor,p._policy);
			this._edc_restoreSource(p._policy);
			if(commit)this.edc_valueToSource(value,p.source);
			p.source=null;
			return true;
		},
		/**override function**/
		edc_editable:function(source){
			return true;
		},
		edc_editing:function(){
			var p=this.edc_properties;
			return (p.source && p.editor && p.editor.editor_editing());
		},
		edc_valueFromSource:function(source){
			if(this.$P.dmName && this['mvc_valueFromModel'])
				return this.mvc_valueFromModel(this.mvc_getModel());
			if(this['mvc_valueFromView'])return this.mvc_valueFromView();
			return null;
		},
		edc_valueToSource:function(value,source){
			if(this.$P.dmName && this['mvc_valueToModel'])
				 this.mvc_valueToModel(value,this.mvc_getModel());
			else if(this['mvc_valueToView'])this.mvc_valueToView(value);
		},
		edc_policy:function(source){
			return xlv.imp.editor.pub.policy;
		},
		_edc_storeSource:function(policy){
			var p=this.edc_properties;
			var dim=Element.getDimensions(p.source);
			var lt=this.getDesktop().viewportOffset(p.source);
			policy.location={left:lt.left,top:lt.top,width:dim.width,height:dim.height};
			var l,t,r,b,w=dim.width,h=dim.height;
			if(l=Element.getStyle(p.source,'paddingLeft'))w-=parseInt(l);
			if(r=Element.getStyle(p.source,'paddingRight'))w-=parseInt(r);
			if(t=Element.getStyle(p.source,'paddingTop'))h-=parseInt(t);
			if(b=Element.getStyle(p.source,'paddingBottom'))h-=parseInt(b);
			policy.inset={width:w,height:h};
			if(policy.install=='replace'){
				p._backup=document.createDocumentFragment();
				while(p.source.firstChild)p._backup.appendChild(p.source.firstChild);
			}
		},
		edc_installEditor:function(value,policy){
			var p=this.edc_properties;
			if(p.editor){
				if(policy.install=='attach'){
					if(p.editor.parentNode!=this.getDesktop())this.getDesktop().appendChild(p.editor);
					var style={position:'absolute',zIndex:xlv.sys.zIndexSystemModal,display:''};
					var dim=Element.getDimensions(p.editor);
					var loc=policy.location;
					if(policy.align=='left'){
						style.left=(loc.left+policy.offsetLeft)+'px';
						style.top=(loc.top+policy.offsetTop+loc.height)+'px';
					}else if(policy.align=='right'){
						style.left=(loc.left+loc.width-dim.width-policy.offsetLeft)+'px';
						style.top=(loc.top+policy.offsetTop+loc.height)+'px';
					}else if(policy.align=='middle'){
						style.left=(loc.left+(loc.width-dim.width)/2)+'px';
						style.top=(loc.top+policy.offsetTop+loc.height)+'px';
					}else if(policy.align=='center'){
						var dsk=Element.getDimensions(this.getDesktop());
						style.left=((dsk.width-dim.width)/2)+'px';
						style.top=((dsk.height-dim.height)/2)+'px';
					}
					Element.setStyle(p.editor,style);
				}
			}
			return p.editor;
		},
		_edc_restoreSource:function(policy){
			var p=this.edc_properties;
			if(policy.install=='replace'){
				if(p._backup)while(p._backup.firstChild)p.source.appendChild(p._backup.firstChild);
				p._backup=null;
			}
		},
		edc_disposeEditor:function(editor,policy){
			var p=this.edc_properties;
			if(policy.dispose=='dispose'&&policy.install!='viewer'){
				p.editor.editor_dispose();
				xlv.sys.dispose(p.editor);
			}
		},
		edc_uninstallEditor:function(editor,policy){
			var p=this.edc_properties;
			if(policy.uninstall=='remove'&&p.editor.parentNode&&policy.install!='viewer') p.editor.parentNode.removeChild(p.editor);
			p.editor=null;
		},
		edc_validate:function(){
			return this.edc_properties.editor.editor_validate();
		},
		edc_cancel:function(){
			this.edc_complete(false);
		},
		edc_stop:function(){
			this.edc_complete(true);
		}
}
};

